package com.netcracker.util.dao.impl;

import com.netcracker.db.exception.DBException;
import com.netcracker.db.DBManager;
import com.netcracker.entity.Language;
import com.netcracker.util.dao.DAOFacade;
import com.netcracker.util.dao.LanguageDAO;
import com.netcracker.util.dao.closer.DAOCloseHelper;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LanguageDAOImpl extends DAOCloseHelper implements LanguageDAO {
    protected static final String LANGUAGE_ID_VALUE = "LanguageId";
    protected static final String LANGUAGE_NAME_VALUE = "LanguageName";

    protected static final String FIND_ALL_LANGUAGES_SQL = "SELECT LanguageId, LanguageName FROM LANGUAGES";
    protected static final String FIND_LANGUAGE_BY_ID_SQL = "SELECT LanguageId, LanguageName FROM LANGUAGES WHERE LanguageId = ?";
    protected static final String FIND_LANGUAGES_COUNT_SQL = "SELECT COUNT(*) FROM LANGUAGES";

    private DBManager dbManager;
    private DAOFacade daoFacade;
    
    public LanguageDAOImpl(DBManager dbManager, DAOFacade daoFacade) {
        this.dbManager = dbManager;
        this.daoFacade = daoFacade;
    }
        
    @Override
    public void setDAOFacade(DAOFacade daoFacade) {
        this.daoFacade = daoFacade;
    }
    
    @Override
    public void setDBManager(DBManager dbManager) {
        this.dbManager = dbManager;
    }

    @Override
    public Language find(int id) throws DBException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        Language language = null;
        try
        {
            preparedStatement = dbManager.prepareStatement(FIND_LANGUAGE_BY_ID_SQL);
            preparedStatement.setInt(1, id);
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next())
            {
                language = getLanguageEntity(resultSet);
            }

        }
        catch (SQLException ex)
        {
            throw new DBException(ex.getMessage(), ex);
        }
        finally {
            closeResultSet(resultSet);
            closeStatement(preparedStatement);
        }
        return language;
    }

    @Override
    public List<Language> findAll() throws DBException {
        ResultSet resultSet = dbManager.execSQL(FIND_ALL_LANGUAGES_SQL);
        List<Language> languages = new ArrayList<Language>();
        try
        {
            while (resultSet.next())
            {
                languages.add(getLanguageEntity(resultSet));
            }

        }
        catch (SQLException ex)
        {
            throw new DBException(ex.getMessage(), ex);
        }
        finally {
            closeResultSet(resultSet);
        }
        return languages;

    }

    @Override
    public int count() throws DBException {
        ResultSet resultSet = null;
        int count = 0;
        try
        {
            resultSet = dbManager.execSQL(FIND_LANGUAGES_COUNT_SQL);
            while (resultSet.next())
            {
                count = resultSet.getInt(1);
            }

        }
        catch (SQLException ex)
        {
            throw new DBException(ex.getMessage(), ex);
        }
        finally {
            closeResultSet(resultSet);
        }
        return count;
    }


    private Language getLanguageEntity(ResultSet resultSet) throws SQLException, DBException
    {
        Language language = new Language();
        language.setId(resultSet.getInt(LANGUAGE_ID_VALUE));
        language.setName(resultSet.getString(LANGUAGE_NAME_VALUE));
        return language;
    }

    
}
